package com.example.imserver.controller;

import com.example.imserver.entity.Message;
import com.example.imserver.service.MessageService;
import com.example.imserver.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/messages")
public class MessageController {
    @Autowired
    private MessageService messageService;
    
    private Long getUserIdFromRequest(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
            return JwtUtil.getUserIdFromToken(token);
        }
        return null;
    }
    
    @PostMapping("/send")
    public Map<String, Object> sendMessage(@RequestBody Message msg, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        msg.setSenderId(userId);
        Message savedMessage = messageService.sendMessage(msg);
        
        result.put("success", true);
        result.put("message", savedMessage);
        return result;
    }
    
    @GetMapping("/conversation/{otherUserId}")
    public Map<String, Object> getConversation(@PathVariable Long otherUserId, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        List<Message> messages = messageService.getConversation(userId, otherUserId);
        result.put("success", true);
        result.put("messages", messages);
        return result;
    }
    
    @GetMapping("/recent")
    public Map<String, Object> getRecentChats(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        List<Message> messages = messageService.getRecentChats(userId);
        result.put("success", true);
        result.put("messages", messages);
        return result;
    }
    
    @GetMapping("/unread")
    public Map<String, Object> getUnreadMessages(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        List<Message> messages = messageService.getUnreadMessages(userId);
        result.put("success", true);
        result.put("messages", messages);
        return result;
    }
    
    @DeleteMapping("/{messageId}")
    public Map<String, Object> deleteMessage(@PathVariable Long messageId, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        messageService.deleteMessage(messageId);
        result.put("success", true);
        return result;
    }
    
    @PutMapping("/{messageId}/read")
    public Map<String, Object> markAsRead(@PathVariable Long messageId, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        messageService.markAsRead(messageId);
        result.put("success", true);
        return result;
    }
    
    @PutMapping("/conversation/{otherUserId}/read")
    public Map<String, Object> markConversationAsRead(@PathVariable Long otherUserId, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Long userId = getUserIdFromRequest(request);
        if (userId == null) {
            result.put("success", false);
            result.put("message", "未登录");
            return result;
        }
        
        messageService.markConversationAsRead(userId, otherUserId);
        result.put("success", true);
        return result;
    }
}

